package com.supermap.dialogs;

import com.supermap.data.DatasetVector;
import com.supermap.data.FieldInfo;
import com.supermap.data.FieldInfos;
import com.supermap.data.FieldType;
import com.supermap.dialogs.advance.CJDialog;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;

public class FieldInfoManageDialog extends CJDialog {
    private DatasetVector datasetVector;

    public FieldInfoManageDialog(Frame owner, String title, boolean modal, DatasetVector datasetVector) {
        super(owner, title, modal, 541, 400, new Object[]{datasetVector});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.datasetVector = (DatasetVector) args[0];
    }

    @Override
    protected void initializeCore() {
        super.confirmBtn.setVisible(false);

        // 属性结构管理工具条
        JToolBar toolBar = new JToolBar();
        add(toolBar, BorderLayout.NORTH);

        JTable fieldInfoTable = new JTable();

        JButton addButton = new JButton();
        addButton.setText("新建字段");
        addButton.addActionListener(e -> {
            FieldInfoAddedDialog newFieldDialog = new FieldInfoAddedDialog((Frame) getOwner(), "新建字段", true);
            newFieldDialog.setVisible(true);
            FieldInfo fieldInfo = newFieldDialog.getFieldInfo();
            newFieldDialog.dispose();
            if (fieldInfo == null) {
                return;
            }

            FieldInfos fieldInfos = datasetVector.getFieldInfos();
            fieldInfos.add(fieldInfo);
            DefaultTableModel model = (DefaultTableModel) fieldInfoTable.getModel();
            reloadFieldInfos(model);
        });
        toolBar.add(addButton);

        JButton deleteButton = new JButton();
        deleteButton.setText("删除字段");
        deleteButton.addActionListener(e -> {
            int selectedRow = fieldInfoTable.getSelectedRow();
            if (selectedRow == -1) {
                return;
            }
            FieldInfos fieldInfos = datasetVector.getFieldInfos();
            fieldInfos.remove(selectedRow);
            DefaultTableModel model = (DefaultTableModel) fieldInfoTable.getModel();
            reloadFieldInfos(model);
        });
        toolBar.add(deleteButton);

        // 属性结构表
        JScrollPane tablePanel = new JScrollPane(fieldInfoTable);
        add(tablePanel, BorderLayout.CENTER);

        DefaultTableModel tableModel = new DefaultTableModel();
        tableModel.addTableModelListener(e -> {
            int column = e.getColumn();
            int row = e.getLastRow();
            if (column == -1 || row == -1) {
                return;
            }

            Object newValue = tableModel.getValueAt(row, column);
            FieldInfos fieldInfos = datasetVector.getFieldInfos();
            FieldInfo fieldInfo = fieldInfos.get(row);
            switch (column) {
                case 1:
                    fieldInfo.setName((String) newValue);
                    break;
                case 2:
                    fieldInfo.setCaption((String) newValue);
                    break;
                case 3:
                    fieldInfo.setType((FieldType) FieldType.parse(FieldType.class, (String) newValue));
                    break;
                case 4:
                    fieldInfo.setMaxLength(Integer.parseInt((String) newValue));
                    break;
                case 5:
                    fieldInfo.setDefaultValue((String) newValue);
                    break;
                case 6:
                    fieldInfo.setRequired(Boolean.getBoolean((String) newValue));
                    break;
                default:
                    break;
            }
        });

        String[] columnNames = new String[]{"", "名称", "别名", "类型", "长度", "缺省值", "必填"};
        tableModel.setColumnIdentifiers(columnNames);
        loadFieldInfos(tableModel);
        fieldInfoTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        fieldInfoTable.setModel(tableModel);
        TableColumnModel columnModel = fieldInfoTable.getColumnModel();
        for (int i = 0; i < columnModel.getColumnCount(); i++) {
            TableColumn column = columnModel.getColumn(i);
            column.setMinWidth(40);
        }
    }

    @Override
    protected void doConfirmCore() {

    }

    private void reloadFieldInfos(DefaultTableModel model) {
        model.getDataVector().clear();
        loadFieldInfos(model);
        model.fireTableDataChanged();
    }

    private void loadFieldInfos(DefaultTableModel tableModel) {
        // 准备表值
        FieldInfos fieldInfos = datasetVector.getFieldInfos();
        int fieldInfosCount = fieldInfos.getCount();
        for (int i = 0; i < fieldInfosCount; i++) {
            FieldInfo fieldInfo = fieldInfos.get(i);
            Object[] tableData = new Object[7];
            tableData[0] = i + 1;
            tableData[1] = fieldInfo.getName();
            tableData[2] = fieldInfo.getCaption();
            tableData[3] = fieldInfo.getType();
            tableData[4] = fieldInfo.getMaxLength();
            tableData[5] = fieldInfo.getDefaultValue();
            tableData[6] = fieldInfo.isRequired();
            tableModel.addRow(tableData);
        }
    }
}
